home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / npns.lha / npns / sendnews.c < prev    next >
C/C++ Source or Header  |  1996-04-20  |  8KB  |  305 lines

  1.  
  2. /*
  3.  *    Program     sendnews
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        06/08/95
  6.  *
  7.  *  Synopsis:    This program sends news which has been spooled by
  8.  *        postnews using NNTPpost from INETUtils. If not 
  9.  *              online then the program will exit and do nothing.
  10.  *              'Onlineness' env var ONLINE
  11.  *
  12.  *              This program was adapted from the postnewsspool
  13.  *              package by James Burton which although useful
  14.  *        was bugged. It could often hang, it gave little
  15.  *              info as what it was doing, it left locks on the
  16.  *              spool dir and was over complicated.
  17.  * 
  18.  *              It is specifically compiled for my INET set up
  19.  *              i.e. spool dir AmiTCP:Usr/Spool/News
  20.  *                   NNTPPost  AmiTCP:bin/NNTPPost
  21.  *             failed arc AmiTCP:Usr/Spool/sendnews.failed
  22.  *
  23.  *        These may be changed using the ENV variables
  24.  *              PNS_SPOOLDIR and PNS_SEQFILE
  25.  *
  26.  *        If NNTPpost fails the article is appended to
  27.  *              the file AmiTCP:Usr/Spool/sendnews.failed or
  28.  *              the ENV variable PNS_FAILED_ARC. Each file has
  29.  *        a header with the date the file failed.
  30.  *
  31.  *  Command Line Arguments: None
  32.  *
  33.  *  Inputs:    Needs enviroment variables NNTPSERVER
  34.  *                       ONLINE
  35.  *                                         PNS_SPOOLDIR   (opt)
  36.  *                                         PNS_NNTPPOST   (opt)
  37.  *                                         PNS_FAILED_ARC (opt)
  38.  *                              
  39.  *
  40.  *  Outputs:    None
  41.  *
  42.  *  Variables:    BPTR    lock         -    lock on spool dir
  43.  *        BPTR    old_lock    -    lock on old current dir
  44.  *        char    *Buffer        -    string to build command in
  45.  *        char    *spooldir    -    spool dir name
  46.  *        char    *nntppost    -    nntppost prog name
  47.  *        char     *failed_arc    -    failed archive filename 
  48.  *        int    count        -    number of articles sent
  49.  *        int    exit_code    -    prog return code
  50.  *        FILE    *fptr        -    file pointer to TCP:daytime port
  51.  *            *fib        -    file info block pointer    
  52.  *
  53.  *  Functions:    save_failed        -    saves failed messages to failed archive
  54.  *
  55.  * $VER: sendnews.c 2.1 (07.08.95) $        
  56.  * $Log: sendnews.c $
  57.  * Revision 4.2  1996/04/20  13:32:57  nagd
  58.  * online check now with env var ONLINE
  59.  *
  60.  * Revision 4.1  1995/12/19  17:56:32  nagd
  61.  * bumped version num
  62.  *
  63.  * Revision 3.1  1995/09/09  20:44:51  daltern
  64.  * REWRITE: Now uses ADOS  functions
  65.  * does not need seqfile anymore
  66.  * will send anything that is the spool dir whatever the name
  67.  * failed arc now separates files with a date string containing
  68.  * the date the file failed
  69.  * New env variable user can now specify nntpposting prog
  70.  *
  71.  * Revision 2.3  1995/09/09  20:41:19  daltern
  72.  * now opens dos library version >37
  73.  *
  74.  * Revision 2.2  1995/08/27  21:32:56  daltern
  75.  * fixed a couple of typos
  76.  *
  77.  * Revision 2.1  1995/08/27  21:29:18  daltern
  78.  * Added some ENV var checking for paths and files
  79.  * Fixed a bug that left a file open
  80.  *
  81.  * Revision 1.3  1995/08/07  13:07:23  daltern
  82.  * added saving of failed messages
  83.  *
  84.  * Revision 1.2  1995/08/07  12:48:19  daltern
  85.  * Added version string
  86.  *
  87.  * Revision 1.1  1995/08/07  12:45:05  daltern
  88.  * Initial revision
  89.  *
  90.  *
  91.  */
  92.  
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95. #include <string.h>
  96. #include <dos/dos.h>
  97. #include <clib/dos_protos.h>
  98. #include <exec/types.h>
  99.  
  100. #define SPOOLDIR    "AmiTCP:Usr/Spool/News"
  101. #define FAILED_ARC  "AmiTCP:Usr/Spool/sendnews.failed"
  102. #define NNTPSERVER  NNTPServer
  103. #define NNTPPOSTER  "AmiTCP:bin/NNTPPost"
  104.  
  105. void save_failed( char *, char * );
  106.  
  107. extern char *version = "$VER: sendnews 4.2 (20.04.95) Nick d'Alterio";
  108.  
  109. int main( void )
  110.  
  111. {
  112.  
  113.   int count;
  114.   int con_status;
  115.   int exit_code = 0;
  116.  
  117.   char *spooldir;
  118.   char *failed_arc;
  119.   char *nntppost;
  120.   char *online;
  121.  
  122.   char Buffer[128];
  123.  
  124.   BPTR lock;
  125.   BPTR old_lock;
  126.  
  127.   struct FileInfoBlock *fib;
  128.  
  129.   if ( ( online = getenv( "ONLINE" ) ) == NULL ) {
  130.     fprintf( stderr, " Please set ONLINE env variable\n" );
  131.     exit(exit_code);
  132.   } else {
  133.     if ( !(con_status = atoi( online )) ) {
  134.         fprintf( stderr, " You are not online !\n" );
  135.         exit(exit_code);
  136.     }   /* end if */
  137.   }   /* end if */
  138.  
  139. /*
  140.  *   Read env variable to get NNTP server.
  141.  */
  142.         
  143.   if ( getenv("NNTPSERVER") == NULL ) {
  144.     fprintf( stderr, " You must specify a NNTP news server\n" );
  145.     exit(10);
  146.   }   /* end if */
  147.  
  148. /*
  149.  *   Read name of NNTP posting program 
  150.  */
  151.  
  152.   if ( ( nntppost = getenv( "PNS_NNTPPOST" ) ) == NULL ) {
  153.     nntppost = NNTPPOSTER;
  154.   }   /* end if */
  155.  
  156. /*
  157.  *   Read spool dir env variable.
  158.  */
  159.  
  160.   if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
  161.     spooldir = SPOOLDIR;
  162.   }   /* end if */
  163.  
  164. /*
  165.  *    Read failed archive name from env variable.
  166.  */
  167.  
  168.   if ( ( failed_arc = getenv( "PNS_FAILED_ARC" ) ) == NULL ) {
  169.     failed_arc = FAILED_ARC;
  170.   }   /* end if */
  171.  
  172. /*
  173.  *   Lock the spool directory whilst posting.
  174.  */
  175.  
  176.     if ( lock = Lock( spooldir, ACCESS_READ ) ) {
  177.  
  178. /*
  179.  *   Change current dir to spool dir and alloc fib
  180.  */
  181.  
  182.           old_lock = CurrentDir( lock );
  183.       
  184.         if ( fib = AllocDosObject( DOS_FIB, NULL ) ) {
  185.  
  186. /*
  187.  *   Examine all the contents of spool dir and try to post
  188.  *   every message.
  189.  */
  190.    
  191.               count = 0;
  192.               Examine( lock, fib );
  193.               while( ExNext( lock, fib ) ) {
  194.  
  195.                 count++;
  196.                 sprintf( Buffer, "%s >NIL: %s", nntppost, fib->fib_FileName );
  197.     
  198.                 if ( !( system( Buffer ) ) ) {
  199.                     fprintf( stderr, " Posted article %d\n", count ); 
  200.                 } else {
  201.                     fprintf( stderr, " Failed to post article %d - saving \n", count );
  202.                     save_failed( fib->fib_FileName, failed_arc );
  203.                 }   /* end if */
  204.  
  205.                 if ( !( DeleteFile( fib->fib_FileName ) ) )
  206.                     PrintFault( IoErr(), fib->fib_FileName );
  207.                       
  208.               }   /* end while */
  209.  
  210.               FreeDosObject( DOS_FIB, fib );
  211.  
  212.         } else {
  213.  
  214.             exit_code = 20;
  215.     
  216.         }   /* end if alloc dos obj */
  217.  
  218.           CurrentDir( old_lock );
  219.         UnLock( lock );
  220.  
  221.     } else {
  222.  
  223.         PrintFault( IoErr(), NULL );
  224.         exit_code = 10;    
  225.  
  226.     }   /* end if lock */
  227.                 
  228.   exit(exit_code);
  229.  
  230. }   /* end program sendnews */
  231.  
  232. /*========================================================================*
  233.                                   END
  234.  *========================================================================*/
  235.  
  236. /*========================================================================*
  237.                           BEGIN FUNCTION save_failed
  238.  *========================================================================*/
  239.  
  240. void save_failed( char *failed_file, char *arc_file )
  241.  
  242. {
  243.  
  244.   FILE *arc_fp;
  245.   FILE *msg_fp;
  246.  
  247.   struct DateStamp ds;
  248.   struct DateTime *dt;
  249.  
  250.   int ch;
  251.  
  252.   if ( !( arc_fp = fopen( arc_file, "a" ) ) ) {
  253.     fprintf( stderr, " Could not open failed archive\n" );
  254.   }   /* end if */
  255.  
  256.   if ( !( msg_fp = fopen( failed_file, "r" ) ) ) {
  257.     fprintf( stderr, " Could not open message file to backup\n" );
  258.     fclose( arc_fp );
  259.     return;
  260.   }   /* end if */
  261.  
  262. /*
  263.  *   Print a header for failed file containing date.
  264.  */
  265.  
  266.   dt = ( struct DateTime *)malloc( sizeof( struct DateTime ) );
  267.  
  268.   dt->dat_StrDay  = (char *)malloc( 20 * sizeof( char ) );
  269.   dt->dat_StrTime = (char *)malloc( 20 * sizeof( char ) );
  270.   dt->dat_StrDate = (char *)malloc( 20 * sizeof( char ) );
  271.   
  272.   dt->dat_Format =  FORMAT_DOS;
  273.   dt->dat_Flags  =  NULL;
  274.  
  275.   DateStamp( &ds );
  276.   dt->dat_Stamp = ds;
  277.   DateToStr( dt );
  278.  
  279.   fprintf( arc_fp, "\n\n ********************* %s %s %s ******************* \n\n",
  280.                                 dt->dat_StrTime, 
  281.                                 dt->dat_StrDay, 
  282.                                 dt->dat_StrDate );
  283.  
  284.   free( dt->dat_StrTime );
  285.   free( dt->dat_StrDay );
  286.   free( dt->dat_StrDate );
  287.   free( dt );
  288.  
  289. /*
  290.  *   Now copy msg to archive
  291.  */
  292.  
  293.   while ( ( ch = fgetc( msg_fp ) ) != EOF ) fputc( ch, arc_fp );
  294.  
  295.  
  296.   fclose( arc_fp );
  297.   fclose( msg_fp );
  298.   return;
  299.  
  300. }   /* end function save_failed */
  301.  
  302. /*========================================================================*
  303.                                  END save_failed
  304.  *========================================================================*/
  305.